import { createWebHashHistory, createRouter } from 'vue-router'
import { isMobile } from '@/utils/index.js'

import Login from '@/views/login/index.vue'
import Register from '@/views/register/index.vue'
import Home from '@/views/index/index.vue'
import ContractTrading from '@/views/contractTrading/index.vue'
import OptionTrading from '@/views/optionTrading/index.vue'
import AiTrading from '@/views/aiTrading/index.vue'
import Account from '@/views/account/index.vue'
import DepositList from '@/views/depositList/index.vue'
import WithdrawList from '@/views/withdrawList/index.vue'
import Record from '@/views/record/index.vue'
import Notification from '@/views/notification/index.vue'
import InviteFriends from '@/views/inviteFriends/index.vue'
import Faq from '@/views/faq/index.vue'
import Verification from '@/views/verification/index.vue'
import Webview from '@/views/webview/index.vue'
import Language from '@/views/language/index.vue'
import AiTradingAdvantages from '@/views/aiTradingAdvantages/index.vue'
import XautAdvantages from '@/views/xautAdvantages/index.vue'
import AiTradingConfirmation from '@/views/aiTradingConfirmation/index.vue'
import OptionTradingAdvantages from '@/views/optionTradingAdvantages/index.vue'
import Deposit from '@/views/deposit/index.vue'
import Proof from '@/views/proof/index.vue'
import Withdraw from '@/views/withdraw/index.vue'




const routes = [
    {
        path: '/',
        redirect: 'home'
    },
    {
        name: 'home',
        path: '/home',
        component: Home
    },
    {
        name: 'login',
        path: '/login',
        component: Login
    },
    {
        name: 'register',
        path: '/register',
        component: Register
    },
    {
        name: 'contractTrading',
        path: '/contractTrading',
        component: ContractTrading
    },
    {
        name: 'optionTrading',
        path: '/optionTrading',
        component: OptionTrading
    },
    {
        name: 'aiTrading',
        path: '/aiTrading',
        component: AiTrading
    },
    {
        name: 'account',
        path: '/account',
        component: Account
    },
    {
        name: 'depositList',
        path: '/depositList',
        component: DepositList
    },
    {
        name: 'withdrawList',
        path: '/withdrawList',
        component: WithdrawList
    },
    {
        name: 'record',
        path: '/record/:index?',
        component: Record
    },
    {
        name: 'notification',
        path: '/notification',
        component: Notification
    },
    {
        name: 'inviteFriends',
        path: '/inviteFriends',
        component: InviteFriends
    },
    {
        name: 'faq',
        path: '/faq',
        component: Faq
    },
    {
        name: 'verification',
        path: '/verification',
        component: Verification
    },
    {
        name: 'webview',
        path: '/webview',
        component: Webview
    },
    {
        name: 'language',
        path: '/language',
        component: Language
    },
    {
        name: 'aiTradingAdvantages',
        path: '/aiTradingAdvantages',
        component: AiTradingAdvantages
    },
    {
        name: 'xautAdvantages',
        path: '/xautAdvantages',
        component: XautAdvantages
    },
    {
        name: 'aiTradingConfirmation',
        path: '/aiTradingConfirmation/:index?',
        component: AiTradingConfirmation
    },
    {
        name: 'optionTradingAdvantages',
        path: '/optionTradingAdvantages',
        component: OptionTradingAdvantages
    },
    {
        name: 'deposit',
        path: '/deposit',
        component: Deposit
    },
    {
        name: 'proof',
        path: '/proof',
        component: Proof
    },
    {
        name: 'withdraw',
        path: '/withdraw',
        component: Withdraw
    },
    
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach(async (to, from, next) => {
    let hasLogin = localStorage.getItem('token') || ''
    if( !hasLogin && to.name !== 'login' && to.name !== 'register'){
        next({ name: 'login'})
    }else {
        if(!isMobile()) {
            if(to.name === 'account') {
                next({ name: 'depositList' })
            }
        }
        window.scrollTo(0, 0);
        next()
    }
})

export default router